#include <stdio.h>  
#include <signal.h>

#ifdef WIN32
#define WIN32_LEAN_AND_MEAN  
#include <windows.h>  
#include <winsock2.h>  
#endif

#include <event2/thread.h>
#include <zsystem/service/batch_httpclient/batch_httpclient.h>
#include <zsystem/core/common/logdump.h>

#ifdef WIN32
#define SERVER_ADDRESS "192.168.2.135"
#define SERVER_PORT 8080
#define THREAD_NUMBER 2
#define BACKLOG 100
#else
#define SERVER_ADDRESS "0.0.0.0"
#define SERVER_PORT 8080
#define THREAD_NUMBER 2
#define BACKLOG 100
#endif

Z_BOOL g_exit=Z_FALSE;
void
sigint(int s) {
  g_exit = true;
}

int
main(int argc, char ** argv)
{
  google::InitGoogleLogging(argv[0]);

#ifdef WIN32
  google::SetLogDestination(INFO, "..\\..\\..\\logs\\batch_httpclient_INFO_");
  google::SetLogDestination(WARNING, "..\\..\\..\\logs\\batch_httpclient_INFO_");
  google::SetLogDestination(ERROR, "..\\..\\..\\logs\\batch_httpclient_INFO_");

  SetStdHandle(STD_ERROR_HANDLE, 0);

  WSADATA wsaData;  
  DWORD Ret;  
  if ((Ret = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0)  {  
    printf("WSAStartup failed with error %d\n", Ret);  
    return -1;  
  }
  CHECK_EQ(evthread_use_windows_threads(), 0);
#else
  google::SetLogDestination(INFO, "/tmp/batch_httpclient_INFO_");
  google::SetLogDestination(WARNING, "/tmp/batch_httpclient_INFO_");
  google::SetLogDestination(ERROR, "/tmp/batch_httpclient_INFO_");

  CHECK_EQ(evthread_use_pthreads(), 0);
#endif

  Z_DUMP("main():  start");
  zsystem::batchHttpClientM clients(SERVER_ADDRESS, SERVER_PORT, 2, 10);
  Z_DUMP("main(): clients.start()");
  if (!clients.start())
  {
    LOG(ERROR) << "Start client failed";
    return -1;
  }

  signal(SIGINT, sigint);
  Z_DUMP("main():  loop");
  while(!g_exit)
  {
    zsystem::ssleep(5);
  };

  Z_DUMP("main(): clients.stop()");
  clients.stop();

  Z_DUMP("main(): exit");
#ifdef WIN32
  WSACleanup();  
#endif
  return 0;  
}  

